/**
 * Created by zdx  on 2016/8/26.
 *
 *
 */

define(['jquery', 'servies', 'datatables', 'dataTableB',"jqconfirm"], function ($, servies) {
    var allpage = servies.allPage();
    var degbug=true;
    onerror=handleErr;
    localStorage.setItem('url','http://g.fly-cloud.cn/');
    $.ajaxSetup ({
        cache: false //close AJAX cache
    });
    function handleErr(msg,url,l)
    {
        var txt="";
        txt="本页中存在错误。\n\n";
        txt+="错误：" + msg + "\n";
        txt+="URL: " + url + "\n";
        txt+="行：" + l + "\n\n";
        txt+="点击“确定”继续。\n\n";
        console.log(txt);
       // return true
    }
    return {
        server : "ws://g.fly-cloud.cn/wssend/",
        playbackserver : "ws://g.fly-cloud.cn/wstrack/",
        plInterval:0,
        urlLoad: function (op) {
            /*if (allpage.getPage(op.url)) {
                $('.content-wrapper').empty().append(allpage.getPage(op.url));
            }else{
                $.get(op.url, {v: vs}, function (result) {
                    $('.content-wrapper').empty().prepend(result);
                    allpage.setPage(op.url,result);
                });
            }*/
            $.get(op.url, {v: vs}, function (result) {
                $('.content-wrapper').empty().prepend(result);
                allpage.setPage(op.url,result);
            });
        },
        conLog:function (d) {
            if(degbug) console.log(d)
        },
        /* ---- 数据处理 ---- */
         getValues : function (key) {
            if(key){
                return localStorage.getItem(key);
            }else{
                return localStorage.getItem(window.location.href.split('pages/')[1]);
            }
        },
         getToken : function () {
            return localStorage.getItem('token');
        },
         RemoveValue : function (key) {
            localStorage.removeItem(key);
        },
        /*
         * 只有一个参数values, key值从href中解析获取
         */
        //  setValues : function (value) {
        //     localStorage.setItem(window.location.href.split('pages/')[1],value);
        // },
        /*
         * 连个参数，key-value
         */
         setValues : function (key,value) {
            localStorage.setItem(key,value);
        },
        /**
         * 退出系统
         */
        getTitle:function () {
            var _this=this;
            var Title;
            if(this.getValues('title')){
                Title=this.getValues('title');
            }else{
                $.ajax({
                    url:this.getValues('url') + 'config',
                    type:'POST',
                    success:function (result) {
                        Title=result.wname;
                        _this.setValues('title',Title);
                        $('.login-logo,.system_title').text(Title);
                    }
                });
            }
            return Title;
        },
        quit:function () {
            localStorage.clear();
            window.location.href = 'login.html'
        },
         ajax : function(params){
            var _this=this;
            var def = {
                type:'POST',
                url:this.getValues('url'),
                data:{
                    token:this.getToken()
                },
                complete:function (p1,p2) {
                    if(p2 == 'success' && (JSON.parse(p1.responseText).err < 0)){


                        _this.alert({
                            content:JSON.parse(p1.responseText).msg,
                            confirm: function(){
                                localStorage.clear();
                                window.location.href = 'login.html';
                            }
                        });

                    }
                    if(p2 == 'success' && (JSON.parse(p1.responseText).err > 0)){
                        _this.alert({
                            content:JSON.parse(p1.responseText).msg
                        });
                    }
                }

            };
            $.extend(true,def,params);
            if(params.url){
                def.url = this.getValues('url') + def.url;
            }
            return $.ajax(def);
        },
        alert:function (op) {
            var newOP=$.extend({
                title:"提示信息",
                confirmButton: '确定'
            },op);
            $.alert(newOP);
        },
        confirm:function (op) {
            var newOP=$.extend({
                title:"提示信息",
                confirmButton: '是',
                cancelButton: '否'
            },op);
            $.confirm(newOP);
        },
        /**
         * 自定义DataTable
         */
        myDataTable:function ($dom,op) {
            var defaultOP={
                "processing": true,
                "serverSide": true,
                searching: false,
                ordering:  false,
                "lengthChange": true,
                "aLengthMenu": [ 10, 15, 20],
                "oLanguage" : {
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    //"sZeroRecords": "抱歉， 没有找到",
                    "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "sInfoEmpty": "没有数据",
                    "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                    "sZeroRecords": "没有检索到数据",
                    "sSearch": "检索:",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "前一页",
                        "sNext": "后一页",
                        "sLast": "尾页"
                    }

                }
            };
            var newOP=$.extend(defaultOP,op);
            return $dom.DataTable(newOP);
        },
        /**
         * 获取通航公司
         */
        getCompanys:function (cid,key) {
            var _key=key||"name";
            var companys =JSON.parse(this.getValues("companys"));
            var name ="";
            $.each( companys, function(i, v){
                if(v["cid"]==cid){
                    name= v[_key];
                    return false
                }
            });
            return name
        },
        getAllCompanys:function () {
             return JSON.parse(this.getValues("companys"));
        },
        formatCompanysToSelect:function (Companys) {
            var nn=[];//公司临时数组
            $.each(Companys,function (i,v) {
                nn.push({id:v.cid,text:v.name})
            });
            return nn;
        },
        elementType: {
            "0": '起降点',
            "1": '途经点',
            "2": '加油点',
            "3": '机场',
            "253": '圆形空域',
            "254": '航线',
            "255": '多边形空域'
        },
        getElementType:function (key) {
            return this.elementType[key];
        },
        /* ---- 时间格式转换 ---- */
        formatDateFromInt : function(int){
            if(int){
                var now = new Date(parseInt(int) * 1000);
                var year=now.getFullYear(),
                    month=(now.getMonth()<9)?'0'+(now.getMonth()+1):now.getMonth()+1,
                    date=(now.getDate()<10)?'0'+(now.getDate()):now.getDate(),
                    hour=(now.getHours()<10)?('0'+now.getHours()):now.getHours(),
                    minute=(now.getMinutes()<10)?('0'+now.getMinutes()):now.getMinutes(),
                    second=(now.getSeconds()<10)?('0'+now.getSeconds()):now.getSeconds();
                return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
            }else{
                return "";
            }
        },
        formatDateWithoutSFromInt : function(int){
            if(int){
                var now = new Date(parseInt(int) * 1000);
                var year=now.getFullYear(),
                    month=(now.getMonth()<9)?'0'+(now.getMonth()+1):now.getMonth()+1,
                    date=(now.getDate()<10)?'0'+(now.getDate()):now.getDate(),
                    hour=(now.getHours()<10)?('0'+now.getHours()):now.getHours(),
                    minute=(now.getMinutes()<10)?('0'+now.getMinutes()):now.getMinutes();
                return year+"-"+month+"-"+date+" "+hour+":"+minute;
            }else{
                return "";
            }
        },
        /* ---- 时间格式转换 时分秒 ---- */
        formatHMSFromInt : function(int){
            if(int){
                var now = new Date(parseInt(int) * 1000);
                var hour=(now.getHours()<10)?('0'+now.getHours()):now.getHours(),
                    minute=(now.getMinutes()<10)?('0'+now.getMinutes()):now.getMinutes(),
                    second=(now.getSeconds()<10)?('0'+now.getSeconds()):now.getSeconds();
                return hour+":"+minute+":"+second;
            }else{
                return "";
            }
        },
        /* ---- 时间格式转换 年月日 ---- */
        formatYMDFromInt: function(int,flag){
            var defaults_flag = "-";
            if(flag !== undefined){
                defaults_flag = flag;
            }
            if(int){
                var now = new Date(parseInt(int) * 1000);
                var year=now.getFullYear(),
                    month=(now.getMonth()<9)?'0'+(now.getMonth()+1):now.getMonth()+1,
                    date=(now.getDate()<10)?'0'+(now.getDate()):now.getDate();
                return year+defaults_flag+month+defaults_flag+date;
            }else{
                return "";
            }
        },
        formatYMDWithoutLineFromInt : function(int){
            if(int){
                var now = new Date(parseInt(int) * 1000);
                var year=now.getFullYear(),
                    month=(now.getMonth()<9)?'0'+(now.getMonth()+1):now.getMonth()+1,
                    date=(now.getDate()<10)?'0'+(now.getDate()):now.getDate();
                return year+month+date;
            }else{
                return "";
            }
        },
        /* ---- 时间格式转换 ---- */
        /*
            数组去重
         */
        undulpicate: function (array) {
            for (var i = 0; i < array.length; i++) {
                for (var j = i + 1; j < array.length; j++) {
                    //注意 ===
                    if (array[i] === array[j]) {
                        array.splice(j, 1);
                        j--;
                    }
                }
            }
            return array;
        }
    }
});